import calendar_ex.CalendarEx;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;

/**
 *
 * @author peter
 */
public class CalendarController {
    
    protected CalendarEx calendarModel;
    protected IS3Calendar calendarView;
    
    public final int daysPerMonth[] = {31,28,30,31,30,31,30,31,30,31,30,31};
    final static String[] hoursList = {"00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23"};
    final static String[] minutesList = {"00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59"};
    final static String[] repeatEveryOptions = {"NONE", "DAILY", "WEEKLY", "TWO_WEEKLY", "FOUR_WEEKLY"};

    public final String[] months = {
        "January",
        "Febuary",
        "March",
        "April",
        "May",
        "June",
        "July",
        "August",
        "September",
        "October",
        "November",
        "December"
    };
    
    public CalendarController(CalendarEx calendarModel, IS3Calendar calendarView) {
        this.calendarModel = calendarModel;
        this.calendarView = calendarView;
    }
    
    public void changeView(IS3Calendar.View view) {
        this.calendarView.changeView(view);
    }
    
    public void updateTitle() {
        IS3Calendar.View view = this.calendarView.getCurrentView();
        MainTemplatePanel t = (MainTemplatePanel) this.calendarView.getTemplateView();
        
        int day = calendarModel.getViewDate().getDay();
        int month = calendarModel.getViewDate().getMonth();
        int year = calendarModel.getViewDate().getYear();
        
        if (view == IS3Calendar.View.DAY) {
            t.setTitle(day + " " + months[month - 1] + " " + year);
        } else if (view == IS3Calendar.View.MONTH) {
            t.setTitle(months[month - 1] + " " + year);
        } else if (view == IS3Calendar.View.WEEK) {
            int week = (int)Math.ceil((double)day / 7.0);
            
            t.setTitle("Week " + week + " of " + months[month - 1] + " " + year);
        } else if (view == IS3Calendar.View.YEAR) {
            t.setTitle(new Integer(year).toString());
        }
    } 
    
    // Getters for inner class listeners
    
    public AddEventListener createAddEventListener() {
        return new AddEventListener();
    }
    
    public ShowFilterListener createShowFilterListener() {
        return new ShowFilterListener();
    }
    
    public PinchInListener createPinchInListener() {
        return new PinchInListener();
    }
    
    public PinchOutListener createPinchOutListener() {
        return new PinchOutListener();
    }
    
    public SlideLeftListener createSlideLeftListener() {
        return new SlideLeftListener();
    }
    
    public SlideRightListener createSlideRightListener() {
        return new SlideRightListener();
    }
    
    public ClickOnEventListener createClickOnEventListener() {
        return new ClickOnEventListener();
    }

    ActionListener createBackEventListener() {
        return new BackEventListener();
    }
    
    
    // Listeners
    
    public class BackEventListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            calendarView.changeToPreviousView();
            updateTitle();
        }
    }
    
    public class AddEventListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            calendarView.changeView(IS3Calendar.View.CREATE);
        }
    }
    
    public class ShowFilterListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            calendarView.changeView(IS3Calendar.View.FILTER);
        }
    }
    
    public class PinchOutListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            calendarView.zoomOutView();
            updateTitle();
        }
    }
    
    public class PinchInListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            calendarView.zoomInView();
            updateTitle();
        }
    }
    
	    public class SlideRightListener implements ActionListener {
	        @Override
	        public void actionPerformed(ActionEvent e) {
	            IS3Calendar.View view = calendarView.getCurrentView();
                
                int d = calendarModel.getViewDate().getDay();
                int m = calendarModel.getViewDate().getMonth();
	            int y = calendarModel.getViewDate().getYear();
                    
	            if (view == IS3Calendar.View.MONTH) {
	                m++;
	                if (m > 12) {
	                   m = 1;
					   y++;
                       if (y > 2012) {
                            y = 2012;
                            m = 12;
                            }
                    }
                    d = 1;
	            } else if (view == IS3Calendar.View.WEEK) {
	                d += 7;
	                if (d > daysPerMonth[m-1]) {
	                    m++;
	                    d = 1;
                        if (m > 12) {
                            y++;
                            if (y > 2012) {
                                y = 2012;
                                m = 12;
                            } else {
                                m = 1;
                            }
                        }
	                }     
	              	int week = (int)Math.ceil((double)d / 7.0);
		            d = (week-1)*7+1;       
	            } else if (view == IS3Calendar.View.DAY) {
                    int dpm = daysPerMonth[m-1];
                    if (y == 2012 && m == 2)
                        dpm = 29; // leap years are the worst.
                        
	                d ++;
	                if (d > dpm) {
	                    m++;
	                    d = 1;
	                }
	                if (m > 12) {
                        y++;
                        if (y > 2012) {
                            y = 2012;
                            m = 12;
                        } else {
                            m = 1;
                        }
                    }

	            } else if (view == IS3Calendar.View.YEAR) {
                    y++;
                    if (y > 2012)
                        y = 2012;
                }

                calendarModel.getViewDate().setDay(d);
                calendarModel.getViewDate().setMonth(m);
                calendarModel.getViewDate().setYear(y);
                    
	            System.out.println(calendarModel.getViewDate());
	            calendarView.getTemplateView().update();
	            updateTitle();
	        }
	    }

	    public class SlideLeftListener implements ActionListener {
	        @Override
	        public void actionPerformed(ActionEvent e) {
	            IS3Calendar.View view = calendarView.getCurrentView();
                
                int d = calendarModel.getViewDate().getDay();
                int m = calendarModel.getViewDate().getMonth();
	            int y = calendarModel.getViewDate().getYear();
                
	            if (view == IS3Calendar.View.MONTH) {
	                m--;
	                if (m < 1) {
	                    m = 12;
						y--;
                        if (y < 2011) {
                            y = 2011;
                            m = 1;
                            }
					}
	            } else if (view == IS3Calendar.View.WEEK) {
					d-=7;
	    			if(d < 1) {
						m--;
						if(m < 1) {
							y--;
							if(y < 2011) {
								y = 2011;
								m = 1;
							} else {
								m = 12;
							}
						}
						d = daysPerMonth[m-1];
					}
	                /*d -= 7;
	                if (d < 1) {
	                    m--;
	                    if (m < 1) {
                            y--;
                            if (y < 2011) {
                                y = 2011;
                                m = 1;
                            } else {
                                m = 12;
                            }
                        }
	                    d = daysPerMonth[m-1];
	                }   */
	              	int week = (int)Math.ceil((double)d / 7.0);
		            d = (week-1)*7+1;
	            } else if (view == IS3Calendar.View.DAY) {
                    int dpm = daysPerMonth[m-1];
                    if (y == 2012 && m == 2)
                        dpm = 29; // leap years are the worst.
                        
	                d --;
	                if (d < 1) {
	                    m--;
	                    if (m < 1) {
	                      y--;
                          if (y < 2011) {
                            y = 2011;
                            m = 1;
                          } else {
                            m = 12;
                          }
                        }
                          
                        if (y == 2012 && m == 2)
                            d = 29; // leap years are the worst.
                        else
                            d = daysPerMonth[m-1];
	                }                 
	            } else if (view == IS3Calendar.View.YEAR) {
                    y--;
                    if (y < 2011)
                        y = 2011;
                }
                
                calendarModel.getViewDate().setDay(d);
                calendarModel.getViewDate().setMonth(m);
                calendarModel.getViewDate().setYear(y);
                    
	            System.out.println(calendarModel.getViewDate());
	            calendarView.getTemplateView().update();
	            updateTitle();
	        }
	    }
    
    public class ClickOnEventListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(null, "User clicked on event");
        }
    }

    public class ClickOnSaveListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e){
           // calendarModel.
        }
    }
    
}